import 'package:flutter/material.dart';
import 'package:xsdk_flutter/xsdk/framework/bean/x_bean_data_table.dart';
import 'package:xsdk_flutter/xsdk/framework/page/x_page_state_table.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_color.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_log.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_style.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_widget.dart';

class XDemoStateTable extends XPageTableState {
  XDemoStateTable()
      : super(
    frozenLeftColumnsCount: 2,
    frozenRightColumnsCount: 1,
    tableTitleLabelRowBackgroundColor: Colors.tealAccent,
    frozenDividerColor: XColorUtils.colorCCCCCC,
    tableBorderColor: XColorUtils.colorCCCCCC,
      isShowTableBorder:true,
  );

  @override
  void onStart() {
    setXTitle(titleText: '冻结表格测试', isShowBottomDivider: true);
  }

  @override
  bool getIsUseRoundedRectWidgetBackground() {
    return true;
  }

  @override
  List<XBeanDataTableLabel> getTableColumnLabels() {
    List<XBeanDataTableLabel> list = [];

    if(frozenLeftColumnsCount!=0 || frozenRightColumnsCount!=0 ){

      list.add(XBeanDataTableLabel('序号',
          columnWidth: 50,
          style: XStyleUtils.textStyle_333_14(fontWeight: FontWeight.bold)));
      list.add(XBeanDataTableLabel('名称',
          columnWidth: 50,
          style: XStyleUtils.textStyle_333_14(fontWeight: FontWeight.bold)));
      list.add(XBeanDataTableLabel('数3',
          columnWidth: 60,
          style: XStyleUtils.textStyle_333_14(fontWeight: FontWeight.bold)));
      list.add(XBeanDataTableLabel('数4',
          columnWidth: 60,
          style: XStyleUtils.textStyle_333_14(fontWeight: FontWeight.bold)));
      list.add(XBeanDataTableLabel('数5',
          columnWidth: 60,
          style: XStyleUtils.textStyle_333_14(fontWeight: FontWeight.bold)));
      list.add(XBeanDataTableLabel('数6',
          columnWidth: 60,
          style: XStyleUtils.textStyle_333_14(fontWeight: FontWeight.bold)));
      list.add(XBeanDataTableLabel('数7',
          columnWidth: 60,
          style: XStyleUtils.textStyle_333_14(fontWeight: FontWeight.bold)));
      list.add(XBeanDataTableLabel('数8',
          columnWidth: 60,
          style: XStyleUtils.textStyle_333_14(fontWeight: FontWeight.bold)));
      list.add(XBeanDataTableLabel('数9',
          columnWidth: 60,
          style: XStyleUtils.textStyle_333_14(fontWeight: FontWeight.bold)));
      list.add(XBeanDataTableLabel('操作',
          columnWidth: 80,
          style: XStyleUtils.textStyle_333_14(fontWeight: FontWeight.bold)));

    }else{
      list.add(XBeanDataTableLabel('序号',
          columnWidth: 50,
          style: XStyleUtils.textStyle_333_14(fontWeight: FontWeight.bold)));
      list.add(XBeanDataTableLabel('名称',
          style: XStyleUtils.textStyle_333_14(fontWeight: FontWeight.bold)));
      list.add(XBeanDataTableLabel('数3',
          columnWidth: 60,
          style: XStyleUtils.textStyle_333_14(fontWeight: FontWeight.bold)));
      list.add(XBeanDataTableLabel('操作',
          columnWidth: 80,
          style: XStyleUtils.textStyle_333_14(fontWeight: FontWeight.bold)));
    }



    return list;
  }

  @override
  Future<void> updateData({int pageIndex = 1}) async {
    setState(() {
      XLogUtils.printLog('---pageIndex=$pageIndex');

      if (pageIndex == 1) {
        widget.currentPageIndex = 1;
        widget.listData.clear();
        widget.listData2.clear();

        List<dynamic> row2 = [];
        List<dynamic> row3 = [];

        if(frozenLeftColumnsCount!=0 || frozenRightColumnsCount!=0 ){
          for (int i = 0; i < 10; i++) {
            row2.add(i * 2);
            row3.add(i * 5);
          }
        }else{
          for (int i = 0; i < 4; i++) {
            row2.add(i * 2);
            row3.add(i * 5);
          }
        }

        row2[0] = '合计';
        row3[0] = '合计B';
        widget.listData2.add(row2);
        //widget.listData2.add(row3);
      }

      widget.currentPageIndex += 1;
      widget.totalPages = 10;

      for (int j = 0; j < 20; j++) {
        List<dynamic> row = [];

        if(frozenLeftColumnsCount!=0 || frozenRightColumnsCount!=0 ){
          for (int i = 0; i < 10; i++) {
            row.add(i);
          }
        }else{
          for (int i = 0; i < 4; i++) {
            row.add(i);
          }
        }

        row[0] = j;
        widget.listData.add(row);
      }
    });
  }

  @override
  bool getIsEnableRefreshLoadData() {
    return true;
  }

  @override
  bool getIsEnablePageLoadData() {
    return true;
  }

  @override
  List<Widget> getTableItemRowWidgets(rowItemList) {

    List<Widget> ww = [];

    for (dynamic yy in rowItemList) {
      ww.add(XWidgetUtils.getLayoutContainer(
          height: 48,
          alignment: Alignment.center,
          child: XWidgetUtils.getWidgetText('$yy',
              style:
              XStyleUtils.textStyle_333_14())));
    }
    return ww;

  }

  @override
  List<Widget>? getTableEndFrozenRowWidgets(rowItemList) {
    List<Widget> ww = [];

    for (dynamic yy in rowItemList) {
      ww.add(XWidgetUtils.getLayoutContainer(
          height: 48,
          alignment: Alignment.center,
          child: XWidgetUtils.getWidgetText('$yy',
              style:
              XStyleUtils.textStyle_333_16(fontWeight: FontWeight.bold))));
    }

    return ww;
  }
}
